Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- function exit_error(){
- case "$1" in
- 0)
- notify-send 'Backup Script Failed' 'Reason: Internet is down'
- ;;
- 1)
- notify-send 'Backup Script Failed' 'Reason: Error archiving folder(s)/file(s)'
- ;;
- 2)
- notify-send 'Backup Script Failed' 'Reason: Error encrypting file(s)'
- ;;
- 3)
- notify-send 'Backup Script Failed' 'Reason: Error copying file to myserver.com'
- ;;
- 4)
- notify-send 'Backup Script Failed' 'Reason: md5sum mismatch'
- ;;
- esac
- exit
- }
- function set_vars(){
- case "$1" in
- 0)
- remote_mail_sha512="$(ssh [email protected] 'sha512sum /home/admin/web/myserver.com/public_html/mail.tar.gz.gpg' | grep -oe '[0-9a-f]\{128\}')"
- local_mail_sha512="$(sha512sum '/home/user/mail.tar.gz.gpg' | grep -oe '[0-9a-f]\{128\}')"
- export remote_mail_sha512 local_mail_sha512
- ;;
- 1)
- remote_kdbx_sha512="$(ssh [email protected] 'sha512sum /home/admin/web/myserver.com/public_html/mpw.kdbx' | grep -oe '[0-9a-f]\{128\}')"
- local_kdbx_sha512="$(sha512sum '/home/user/Documents/mpw.kdbx' | grep -oe '[0-9a-f]\{128\}')"
- export remote_kdbx_sha512 local_kdbx_sha512
- ;;
- esac
- }
- sleep 15
- set_display_preferences
- ping 1.1.1.1 -c1 -w1 || exit_error 0
- mail_modified='false'
- for mailbox in /home/user/Mail/*; do
- [[ "$mailbox" -nt '/home/user/mail-last-modified' ]] && mail_modified='true'
- done
- phone_id="$(kdeconnect-cli -l | grep 'Sony.*paired and reachable' | grep -oe '[0-9a-f]\{16\}')"
- set_vars 0
- if "$mail_modified" || [[ "$local_mail_sha512" != "$remote_mail_sha512" ]]; then
- touch '/home/user/mail-last-modified'
- tar czf '/home/user/mail.tar.gz' '/home/user/Mail' || exit_error 1
- gpg --batch --yes --output '/home/user/mail.tar.gz.gpg' --encrypt --recipient user@mydomain.com '/home/user/mail.tar.gz' || exit_error 2
- rm -f '/home/user/mail.tar.gz'
- scp '/home/user/mail.tar.gz.gpg' '[email protected]:/home/admin/web/mydomain.com/public_html/' || exit_error 3
- set_vars 0
- [[ "$local_mail_sha512" == "$remote_mail_sha512" ]] || exit_error 4
- [[ -z "$phone_id" ]] || cp '/home/user/mail.tar.gz.gpg' "/run/user/1000/${phone_id}/storage/emulated/0/Download/mail.tar.gz.gpg"
- uploaded_files=('mail.tar.gz.gpg')
- fi
- set_vars 1
- if [[ "$local_kdbx_sha512" != "$remote_kdbx_sha512" ]]; then
- scp '/home/user/Documents/mpw.kdbx' '[email protected]:/home/admin/web/mydomain.com/public_html/' || exit_error 3
- set_vars 1
- [[ "$local_kdbx_sha512" == "$remote_kdbx_sha512" ]] || exit_error 4
- [[ -z "$phone_id" ]] || cp '/home/user/Documents/mpw.kdbx' "/run/user/1000/${phone_id}/storage/emulated/0/Download/mpw.kdbx"
- uploaded_files+=('mpw.kdbx')
- fi
- if [[ ! -z "${uploaded_files[*]}" ]]; then
- file_list="${uploaded_files[*]}"
- notify-send 'Backup Script Successful' "Successfully uploaded:\\n${file_list// /$'\n'}"
- [[ -z "$phone_id" ]] || kdeconnect-cli -d "$phone_id" --ping-msg "Received files: ${file_list// /, }"
- else
- notify-send 'Backup Script Successful' 'No uploads were needed'
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement